<?php
    require_once("condb.php");
    require_once("functions.php");
    
    $record = 10;
    
    $output = array();
    $info = isset($_GET["info"]) ? $_GET["info"] : "";
    if(!empty($info)){
        $info = urldecode(base64_decode($info));
        parse_str($info, $request);    
        
        $token = isset($request["token"]) ? cleanQuery($request["token"]) : "";
        if($token=="TAoViEC@)!#2012"){
            $data = array();
            
            $data["type"] = isset($request["type"]) ? intval($request["type"]) : 0;                        
            $data["refCode"] = isset($request["refCode"]) ? cleanQuery($request["refCode"]) : "";                        
            $data["os_type"] = isset($request["os_type"]) ? cleanQuery($request["os_type"]) : "";                        
            
            if(!empty($data["type"]) && !empty($data["refCode"]) && !empty($data["os_type"])){
                
                /* Lấy username */
                $user = getUser($data["refCode"]);
                if(!empty($user)){
                    
                    /* Lấy quảng cáo */
                    $adv = getPromotion($data["type"], $data["os_type"]);
                    if(!empty($adv)){
                        $promotion = array();
                        $promotion["src"] = getImage($adv["picture"], "banner", $adv["create_date"], "");
                        $promotion["link"] = "http://" . $user["username"] . ".cucdinh.mobi";   
                    }
                    
                    /* Lấy quảng cáo text */
                    $advText = getPromotionText($data["type"], $data["os_type"]);
                    if(!empty($advText)){
                        $promotionText = array();
                        $promotionText["title"] = $advText["title"];
                        $promotionText["link"] = "http://" . $user["username"] . ".cucdinh.mobi";
                    }
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";    
                } else {
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                }
                    
            } else {
                $output["status"] = 4;
                $output["msg"] = "Invalid Request";                        
            }
                
        } else {
            $output["status"] = 3;
            $output["msg"] = "Invalid Request";    
        }
    } else {
        $output["status"] = 2;
        $output["msg"] = "Invalid Request";
    }                         
    
    $output["promotion"] = isset($promotion) ? $promotion : array();
    $output["promotionText"] = isset($promotionText) ? $promotionText : array();
    
    echo json_encode($output);  
?>